/*
 * Copyright (c) 2010 - 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */


package org.oxtypes.impl;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.omg.dds.type.TypeKind;
import org.omg.dds.type.dynamic.TypeDescriptor;
import org.omg.dds.type.dynamic.DynamicType;
import org.omg.dds.type.dynamic.MemberDescriptor;
import org.omg.dds.type.dynamic.DynamicTypeMember;
import org.omg.dds.type.dynamic.AnnotationDescriptor;

/**
 *
 */

public class DynamicTypeImpl extends DDSObjectImpl implements DynamicType {

    public DynamicTypeImpl(TypeDescriptor td,
            List<DynamicTypeMember> members,
            List<AnnotationDescriptor> annotations){

        // has to be replaced with a new unmodifiable deep copy 
        // at this time, spec does not provide the corresponding
        // method.
        this.descriptor = td;
        
        this.annotations = annotations;

        this.members = new TreeMap<String, DynamicTypeMember>();
        this.memberByIds = new TreeMap<Integer, DynamicTypeMember>();

        if(members == null) return;

        for(DynamicTypeMember dtm : members){
            this.members.put(dtm.getName(), dtm);
            this.memberByIds.put(dtm.getId(), dtm);
        }
    }

    public TypeDescriptor getDescriptor(){
        return this.descriptor;
    }

    public String getName(){
        return this.descriptor.getName();
    }

    public TypeKind getKind(){
        return this.descriptor.getKind();
    }

    public DynamicTypeMember getMember(String name) {
        return this.members.get(name);
    }

    public Map<String, DynamicTypeMember> getAllMembers(){
        return java.util.Collections.unmodifiableMap(this.members);
    }

    public DynamicTypeMember getMemberById(int id){
        return this.memberByIds.get(id);
    }

    public Map<Integer, DynamicTypeMember> getAllMembersById(){
        return java.util.Collections.unmodifiableMap(this.memberByIds);
    }

    public List<AnnotationDescriptor> getAnnotations(){
        return java.util.Collections.unmodifiableList(this.annotations);
    }

    public void applyAnnotation(AnnotationDescriptor descriptor){
        throw new java.lang.UnsupportedOperationException();
    }

    public DynamicTypeMember addMember(MemberDescriptor descriptor){
        throw new java.lang.UnsupportedOperationException();
    }

    public DynamicType clone(){
        throw new java.lang.UnsupportedOperationException();
    }

    // members

    private TypeDescriptor descriptor;

    private Map<String, DynamicTypeMember> members;
    private Map<Integer, DynamicTypeMember> memberByIds;

    private List<AnnotationDescriptor> annotations;
}
